package com.example.administrator.picassodemo;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/8/14.
 */
public class SunAdapter extends BaseAdapter{
    private final ArrayList<SunBean> mList;
    private final Context mContext;

    public SunAdapter(ArrayList<SunBean> listDatas, Context mainActivity) {
        this.mList=listDatas;
        this.mContext=mainActivity;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int i) {
        return mList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ItemViewHolder viewHolder;
        if (convertView==null){
            viewHolder = new ItemViewHolder();
            convertView = View.inflate(mContext, R.layout.activity_main, null);
            viewHolder.mImageview= (ImageView) convertView.findViewById(R.id.image_iv);
            viewHolder.mTextview = (TextView) convertView.findViewById(R.id.text_tv);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ItemViewHolder) convertView.getTag();
        }
        viewHolder.mTextview.setText(mList.get(position).getmTitle());
        /**
         * load url加载地址
         * fit 尝试调整大小至目标界限
         * placeholder 预加载展位图片设置
         * error 加载错误图片占位
         * into 目标图片
         */
        Picasso.with(mContext).load(mList.get(position).getmPicUrl()).fit().placeholder(R.mipmap.image_bg).error(R.mipmap.image_bg)
                .into(viewHolder.mImageview);

        return convertView;
    }

    private class ItemViewHolder {
        private ImageView mImageview;
        private TextView mTextview;
    }
}
